// import utils from '../../utils/util.js'
import api from "../../api/label.js";
import openFileFunc from '../../utils/openFileFunc.js'

// var now = new Date();
// var month = now.getMonth();
// var year = now.getFullYear();
// var keyTime = 'beginDate'
Page({
  data: {
    // beginDate: utils.formatTime(new Date(year, month, 1)),
    // endDate: utils.formatTime(new Date(year, month + 1, 1)),
    // currentDate: new Date(utils.formatTime(new Date(year, month, 1))).getTime(),
    // minDate: new Date('2023/12').getTime(),
    // showSheet: false,
    list: [],
    pageSize: 20, //当前多少页
    pageNo: 0, //现在多少页
    total: 0, //最后一页
    isNoMore: false,
    countNum: 0, // 总数量
    sumPoints: 0, // 总积分
  },
  onLoad(e) {
    // const item = JSON.parse(options.item)
    this.setData({
      batchno: Number(e.batchno),
      // beginDate: item.beginDate,
      // endDate: item.endDate
    }, () => {
      this.getData(1)
    })
  },

  refresh() {
    this.getData(1)
    this.setData({
      triggered: false
    })
  },
  //获取列表接口----page..接受实参
  getData(pageNo) {
    //接口获取
    api.cancelRecordDetailPageByCust({
      pageNo: pageNo, //当前页
      pageSize: this.data.pageSize, //需要多少条数据
      batchno: this.data.batchno // 批次号
    }).then(res => {
      //判断接口返回是否有值
      if (res.code == 0) {
        const eleF = res.data?.find(ele => !ele.dealerName && !ele.breedName)
        console.log(eleF)
        if (pageNo == 1) {
          //赋值
          this.setData({
            list: res.data.filter(ele => ele.breedName && ele.dealerName), //列表数据
            total: res.data.total,
            countNum: eleF.countNum,
            sumPoints: eleF.sumPoints,
            pageNo
          })
        } else {
          //拼接数据concat
          this.setData({
            list: this.data.list.concat(res.data.filter(ele => ele.breedName && ele.dealerName)), //拼接列表数据
            isNoMore: res.data?.length == 0,
            countNum: eleF.countNum,
            sumPoints: eleF.sumPoints,
            pageNo
          })
        }
      } else {
        //否则置空
        this.setData({
          list: [], //置空列表数据
        })
      }
      const disCheckBool = this.data.cancelStatus === 1
      this.setData({
        disCheckBool: disCheckBool,
        triggered: false
      })
    })
  },
  // 触底分页函数
  paging() {
    // 当前页 < 最后页
    if (this.data.pageNo < this.data.total) {
      // 调用列表函数pageNo+1
      this.getData(this.data.pageNo + 1)
    }
  },
  exportRecord() {
    api.exportRecord({
      pageNo: this.data.pageNo, //当前页
      pageSize: this.data.pageSize, //需要多少条数据
      batchno: this.data.batchno // 批次号
    }).then(res => {
      openFileFunc(res, this.data.batchno)
    })
  }
})